<?php
/**
 * @version $Id: trackback_send.php 181 2008-03-11 21:46:28Z mikhail.miguel $
 * @author  Takeshi Kuriyama <kuri@keynext.co.jp>
 * @copyright Copyrighted(c) 2007 by Takeshi Kuriyama <kuri@keynext.co.jp>
 */

$mytrustdirname = basename( dirname( __FILE__ ) ) ;
$mytrustdirpath = dirname( __FILE__ ) ;

// session_start
require $mytrustdirpath.'/lib/session.php';
include_once XOOPS_ROOT_PATH.'/include/common.php';

// GET THIS MODULE HANDLER
require_once $mytrustdirpath.'/include/prepend.inc.php';

// language files
$langmanpath = XOOPS_TRUST_PATH.'/libs/altsys/class/D3LanguageManager.class.php';
if( ! file_exists( $langmanpath ) ) die( 'install the latest altsys' ) ;
require_once( $langmanpath ) ;
$langman =& D3LanguageManager::getInstance() ;
$langman->read( 'main.php' , $mydirname , $mytrustdirname );

//require $mytrustdirpath.'/lib/session.php';
require_once $mytrustdirpath.'/include/form/EntryEditForm.class.php';

$tb_handler =& $myModule->getHandler('trackback');

$form =& D3blogSession::get('entry_form');

if(!count($form->trackback_url_)) {
    header('location: update_ping.php');
    exit;
}

require_once $mytrustdirpath. '/lib/trackback/trackback.php';

$trackback_urls = $form->trackback_url_;
foreach(array_keys($trackback_urls) as $key) {
    $url = $trackback_urls[$key];
    unset($form->trackback_url_[$key]);
    D3blogSession::register('entry_form', $form);

    $trackback = new TrackbackService();

    // fetch and autodiscovery of trackback url
    if(!empty($form->autodiscovery_)) {
        $trackback->_data['url'] = $url;
        $ping_url = $trackback->autodiscover();
        if($trackback->isError($ping_url)) {
            redirect_header('trackback_send.php', 3, $ping_url->getMessage().'<br />'.sprintf(_MD_D3BLOG_MESSAGE_AUTODISCOVERY_FAILED, htmlspecialchars($url)));
        }
        $trackback->_data['trackback_url'] = $ping_url;
    } else {
    	$trackback->_data['trackback_url'] = $url;
    }

    $trackback->_data['url'] = xoops_utf8_encode($form->pingdata_['url']);
    $trackback->_data['title'] = xoops_utf8_encode($form->pingdata_['title']);
    $trackback->_data['blog_name'] = xoops_utf8_encode($form->pingdata_['blog_name']);
    $trackback->_data['excerpt'] = xoops_utf8_encode($form->pingdata_['excerpt']);

    $result = $trackback->send($ping_url);  
    if($trackback->isError($result)) {
        redirect_header('trackback_send.php', 3, $result->getMessage().'<br />'.sprintf(_MD_D3BLOG_MESSAGE_TRACKBACK_FAILED, htmlspecialchars($url)));
    }

    // generate a new trackback object
    $tb_new =& $tb_handler->create();
    $tb_new->setVar('bid', $form->bid_);
    $tb_new->setVar('direction', 1);
    $tb_new->setVar('approved', 1);
    $tb_new->setVar('url', $url);
    $tb_new->setVar('trackback_url', $ping_url);
    if(!$tb_handler->insert($tb_new, true)) {
        d3blog_error(_MD_D3BLOG_MESSAGE_DB_TRACKBACK_FAILED);
    }
        
    unset($tb_new);
    unset($trackback);
}    

if(empty($form->update_ping_)) {
    D3blogSession::unregister('entry_form');
    redirect_header('details.php?bid='.$form->bid_, 1, _MD_D3BLOG_MESSAGE_TRACKBACK_FINISHED_SUCCESSFULLY);
} else {
    redirect_header('update_ping.php', 1, _MD_D3BLOG_MESSAGE_TRACKBACK_FINISHED_SUCCESSFULLY);
    exit;
}
?>